package com.test.controller;


import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class test {
	/** 
	 * 响应验证码页面 
	 * @return 
	 */  
	@RequestMapping(value="/validateCode.do")  
	public String validateCode(javax.servlet.http.HttpServletRequest request,HttpServletResponse response) throws Exception{  
	    // 设置响应的类型格式为图片格式  
	    response.setContentType("image/jpeg");  
	    //禁止图像缓存。  
	    response.setHeader("Pragma", "no-cache");  
	    response.setHeader("Cache-Control", "no-cache");  
	    response.setDateHeader("Expires", 0);  
	  
	    HttpSession session = request.getSession();  
	  
	    ValidateCode vCode = new ValidateCode(120,40,4,100);  
	    session.setAttribute("code", vCode.getCode());  
	    vCode.write(response.getOutputStream());  
	    return null;  
	}  
	@RequestMapping("yan.do")
	public String yanzheng(javax.servlet.http.HttpServletRequest request) {
		String code = request.getParameter("code");
		HttpSession session = request.getSession();
		String sessionCode = (String) session.getAttribute("code");  
		try {
			if (!StringUtils.equalsIgnoreCase(code, sessionCode)) {  //忽略验证码大小写  
			    throw new RuntimeException("验证码对应不上code=" + code + "  sessionCode=" + sessionCode);
			}  
		} catch (Exception e) {
			return "fail";
		}
		
		return "success";
	}
	

}
